package cjakma.study.portfolio.common.validation.constraints.impl;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import cjakma.study.portfolio.common.model.CodeSupport;
import cjakma.study.portfolio.common.validation.constraints.Code;

/**
 * {@link CodeSupport} 구현체의 validator.
 * @author 서성수
 */
public class CodeValidator implements ConstraintValidator<Code, String> {

	private Class<? extends CodeSupport> codeType;

	//@Override
	public void initialize(Code constraint) {
		this.codeType = constraint.value();
	}

	//@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		if (value == null || value.isEmpty())
			return true;
		try {
			CodeSupport.valueOf(codeType, value);
		} catch (IllegalArgumentException ignored) {
			return false;
		}
		return true;
	}

}
